package cn.flexui.flexuiserver.controller.web;

import cn.flexui.flexuiserver.common.ResponseResultGenerator;
import cn.flexui.flexuiserver.domain.ResponseResult;
import cn.flexui.flexuiserver.domain.entity.MemberCard;
import cn.flexui.flexuiserver.domain.entity.MemberRights;
import cn.flexui.flexuiserver.domain.vo.MemberCardVo;
import cn.flexui.flexuiserver.domain.vo.RightConfigVo;
import cn.flexui.flexuiserver.service.MemberCardService;
import cn.flexui.flexuiserver.service.MemberRightsService;
import cn.hutool.core.collection.CollUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 会员卡 前端控制器
 * </p>
 *
 * @author apeto
 * @since 2024-09-22 10:50:52
 */
@Tag(name = "会员")
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberCardService memberCardService;
    @Autowired
    private MemberRightsService memberRightsService;

    @Operation(summary = "会员卡列表")
    @GetMapping("/members")
    public ResponseResult<List<MemberCardVo>> members() {
        List<MemberCard> memberCards = memberCardService.selectByViewTypeAndType();
        List<MemberCardVo> result = memberCards.stream().map(memberCard -> {
            List<MemberRights> memberRights = memberRightsService.getByMemberCode(memberCard.getId());
            if (CollUtil.isNotEmpty(memberRights)) {
                MemberCardVo vo = new MemberCardVo();
                vo.setCardName(memberCard.getCardName());
                vo.setAmount(memberCard.getAmount());
                Integer cardDay = memberCard.getCardDay();
                vo.setCardDay(cardDay);
                if (cardDay < 30) {
                    if (cardDay == 7) {
                        vo.setPeriod("周");
                    } else {
                        vo.setPeriod("天");
                    }
                } else if (cardDay == 30) {
                    vo.setPeriod("月");
                } else if (cardDay < 365) {
                    vo.setPeriod(cardDay / 30 + "月");
                } else if (cardDay == 365) {
                    vo.setPeriod("年");
                } else {
                    vo.setPeriod(cardDay / 365 + "年");
                }

                vo.setLineAmount(memberCard.getLineAmount());
                vo.setRecommend(memberCard.getRecommend());
                vo.setMemberId(memberCard.getId());
                vo.setRightList(memberRights.stream().map(mr -> {
                    RightConfigVo rightConfigVo = new RightConfigVo();
                    rightConfigVo.setRightsDesc(mr.getRightsDesc());
                    rightConfigVo.setRightsName(mr.getRightsName());
                    rightConfigVo.setRightsType(mr.getRightsType());
                    return rightConfigVo;
                }).collect(Collectors.toList()));
                return vo;
            } else {
                return null;
            }

        }).filter(Objects::nonNull).sorted(
                Comparator.comparing(MemberCardVo::getRecommend).reversed().thenComparing(MemberCardVo::getAmount)
        ).collect(Collectors.toList());
        return ResponseResultGenerator.success(result);
    }

}
